# import request
from django.conf import settings
from django.shortcuts import render, redirect
from django.views.generic import View
from django.http import JsonResponse, HttpResponse
from Weifeng_app.models import *
from Weifeng_app.libs import *


# Create your views here.

class Email_RegisterView(View):
    def post(self, request):
        username = request.POST.get("username")
        verCore = request.POST.get("verCore")
        password = request.POST.get("password")
        if not all([username, verCore, password]):
            return JsonResponse({"errno": -1, "errmsg": "请求参数错误"})
        user = User.objects.filter(username=username).first()
        if user:
            return JsonResponse({"errno": -1, "errmsg": "账号已注册"})

        password = sha_pwd(password)
        try:
            user = User(username=username, verCore=verCore, password=password)
            user.save()
        except Exception as e:
            print(e)
            return JsonResponse({"errno": -1, "errmsg": "账号保存异常"})
        return JsonResponse({"errno": 1, "errmsg": "注册成功", "url": "/login/"})
